package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatUserVipLevel;
import com.happy.cat.project.system.service.ICatUserVipLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 会员等级设置Controller
 *
 * @author verne
 * @date 2020-07-18
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/userLevel" )
@Api("会员等级设置 API")
public class CatUserVipLevelController extends BaseController {

    private final ICatUserVipLevelService iCatUserVipLevelService;


    @GetMapping(value = "/all" )
    public AjaxResult all() {
        return AjaxResult.success(iCatUserVipLevelService.list());
    }

    /**
     * 查询会员等级设置列表
     */
    @ApiOperation(value = "查询会员等级设置列表",notes = "查询会员等级设置列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:userLevel:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatUserVipLevel catUserVipLevel)
    {
        startPage();
        LambdaQueryWrapper<CatUserVipLevel> lqw = new LambdaQueryWrapper<CatUserVipLevel>();
        if (StringUtils.isNotBlank(catUserVipLevel.getName())){
            lqw.like(CatUserVipLevel::getName ,catUserVipLevel.getName());
        }
        if (catUserVipLevel.getExperience() != null){
            lqw.eq(CatUserVipLevel::getExperience ,catUserVipLevel.getExperience());
        }
        if (catUserVipLevel.getVipPrivilegeId() != null){
            lqw.eq(CatUserVipLevel::getVipPrivilegeId ,catUserVipLevel.getVipPrivilegeId());
        }
        if (catUserVipLevel.getCreateDate() != null){
            lqw.eq(CatUserVipLevel::getCreateDate ,catUserVipLevel.getCreateDate());
        }
        if (catUserVipLevel.getStatus() != null){
            lqw.eq(CatUserVipLevel::getStatus ,catUserVipLevel.getStatus());
        }
        List<CatUserVipLevel> list = iCatUserVipLevelService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出会员等级设置列表
     */
    @ApiOperation(value = "导出会员等级设置列表",notes = "导出会员等级设置列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userLevel:export')" )
    @Log(title = "会员等级设置" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatUserVipLevel catUserVipLevel) {
        LambdaQueryWrapper<CatUserVipLevel> lqw = new LambdaQueryWrapper<CatUserVipLevel>(catUserVipLevel);
        List<CatUserVipLevel> list = iCatUserVipLevelService.list(lqw);
        ExcelUtil<CatUserVipLevel> util = new ExcelUtil<CatUserVipLevel>(CatUserVipLevel. class);
        return util.exportExcel(list, "userLevel" );
    }

    /**
     * 获取会员等级设置详细信息
     */
    @ApiOperation(value = "获取会员等级设置详细信息",notes = "获取会员等级设置详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userLevel:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatUserVipLevelService.getById(id));
    }

    /**
     * 新增会员等级设置
     */
    @ApiOperation(value = "新增会员等级设置",notes = "新增会员等级设置",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userLevel:add')" )
    @Log(title = "会员等级设置" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatUserVipLevel catUserVipLevel) {
        catUserVipLevel.setCreateDate(new Date());
        catUserVipLevel.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatUserVipLevelService.save(catUserVipLevel) ? 1 : 0);
    }

    /**
     * 修改会员等级设置
     */
    @ApiOperation(value = "修改会员等级设置",notes = "修改会员等级设置",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userLevel:edit')" )
    @Log(title = "会员等级设置" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatUserVipLevel catUserVipLevel) {
        catUserVipLevel.setCreateDate(new Date());
        catUserVipLevel.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatUserVipLevelService.updateById(catUserVipLevel) ? 1 : 0);
    }

    /**
     * 删除会员等级设置
     */
    @ApiOperation(value = "删除会员等级设置",notes = "删除会员等级设置",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userLevel:remove')" )
    @Log(title = "会员等级设置" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatUserVipLevelService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
